﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class product_insurance_user_insurance_terminate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //this.gv_terminate.DataBind();
        //this.gv_InsuranceTransaction.DataBind();
    }

    protected void gv_terminate_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);

        // Retrieve the row that contains the button clicked 
        // by the user from the Rows collection.
        GridViewRow row = gv_terminate.Rows[index];

        int insuredId = int.Parse(row.Cells[0].Text);
        DropDownList reseanDropDownList = (DropDownList)row.Cells[1].FindControl("dl_reason");
        String resean = reseanDropDownList.SelectedItem.Text;
        String savingAccountNo = Session["savingAccountNo"] as String;

        switch (e.CommandName)
        {
            case "teminate":
                terminate(insuredId, resean, savingAccountNo);
                break;
            case "pay":
                pay(insuredId, savingAccountNo);
                break;
        }
        this.gv_terminate.DataBind();
        this.gv_InsuranceTransaction.DataBind();
    }


    private void terminate(int insuredID, String termianteReason,String savingAccountNo)
    {
        TerminateBLL bll = new TerminateBLL();
        bll.terminatePolicy(insuredID, termianteReason, savingAccountNo);
    }


    private void pay(int insuredID,String savingAccountNo){
        RenewBLL bll = new RenewBLL();
        String custId = Session["userId"] as String;
        bll.renew(custId,insuredID, savingAccountNo);
    
    
    }

    protected void gv_terminate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) //to exclude header row 
        {

            DropDownList reasonList = e.Row.FindControl("dl_reason") as DropDownList;

            HiddenField hd_terminateReason = e.Row.FindControl("hd_terminateReason") as HiddenField;
            

            for (int i = 0; i < reasonList.Items.Count; i++)
            {
                if (reasonList.Items[i].Text.Equals(hd_terminateReason.Value))
                {
                    reasonList.Items[i].Selected = true;
                }
            }   

            String policyStatus = e.Row.Cells[1].Text;
            if ("TERMINATED".Equals(policyStatus) || "EXPIRED".Equals(policyStatus))
            {
                e.Row.Cells[3].Enabled = false;
                e.Row.Cells[4].Enabled = false;
                reasonList.Enabled = false;
            }
            else
            {
                e.Row.Cells[3].Enabled = true;
                e.Row.Cells[4].Enabled = true;
                reasonList.Enabled = true;
            }
        }
    }
}